﻿using Eassets.Layered.Domain.Asset;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace Eassets.Data.Asset
{
    public class FixedAssetCustodianData
    {
        /// <summary>
        /// Inserts fixedAssetCustodian.
        /// </summary>
        /// <param name="fixedAssetCustodian"></param>
        public void AddFixedAssetCustodian(FixedAssetCustodian fixedAssetCustodian)
        {
            SqlCommand command = new SqlCommand("SP_AddFixedAssetCustodian"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, fixedAssetCustodian));
        }


        /// <summary>
        /// Get fixedAssetCustodian by Id.
        /// </summary>
        /// <param name="fixedAssetCustodianId"></param>
        /// <returns></returns>
        public FixedAssetCustodian GetByIdFixedAssetCustodian(long fixedAssetCustodianId)
        {
            SqlCommand command = new SqlCommand("SP_GetFixedAssetCustodianById");
            command.Parameters.AddWithValue("@FixedAssetCustodianId", fixedAssetCustodianId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            FixedAssetCustodian fixedAssetCustodian = new FixedAssetCustodian();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["ChangeCustodianId"].ToString())))
                {
                    fixedAssetCustodian.ChangeCustodianId = Convert.ToInt64(dataReader["ChangeCustodianId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    fixedAssetCustodian.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    fixedAssetCustodian.TransactionId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    fixedAssetCustodian.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    fixedAssetCustodian.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    fixedAssetCustodian.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["PastOwner"].ToString())))
                {
                    fixedAssetCustodian.PastOwner = Convert.ToString(dataReader["PastOwner"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewOwner"].ToString())))
                {
                    fixedAssetCustodian.NewOwner = Convert.ToString(dataReader["NewOwner"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EffectiveDate"].ToString())))
                {
                    fixedAssetCustodian.EffectiveDate = Convert.ToDateTime(dataReader["EffectiveDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Status"].ToString())))
                {
                    fixedAssetCustodian.Status = Convert.ToString(dataReader["Status"]);
                }
            }
            dataReader.Close();
            return fixedAssetCustodian;
        }

        /// <summary>
        /// Get All fixedAssetCustodian.
        /// </summary>
        /// <returns></returns>
        public Collection<FixedAssetCustodian> GetAllFixedAssetCustodian()
        {
            Collection<FixedAssetCustodian> fixedAssetCustodians = new Collection<FixedAssetCustodian>();
            SqlCommand command = new SqlCommand("SP_GetAllFixedAssetCustodians");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            FixedAssetCustodian fixedAssetCustodian;
            while (dataReader.Read())
            {
                fixedAssetCustodian = new FixedAssetCustodian();
                if (!(String.IsNullOrEmpty(dataReader["ChangeCustodianId"].ToString())))
                {
                    fixedAssetCustodian.ChangeCustodianId = Convert.ToInt64(dataReader["ChangeCustodianId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    fixedAssetCustodian.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    fixedAssetCustodian.OrganizationLevelListId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    fixedAssetCustodian.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    fixedAssetCustodian.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    fixedAssetCustodian.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["PastOwner"].ToString())))
                {
                    fixedAssetCustodian.PastOwner = Convert.ToString(dataReader["PastOwner"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewOwner"].ToString())))
                {
                    fixedAssetCustodian.NewOwner = Convert.ToString(dataReader["NewOwner"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EffectiveDate"].ToString())))
                {
                    fixedAssetCustodian.EffectiveDate = Convert.ToDateTime(dataReader["EffectiveDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Status"].ToString())))
                {
                    fixedAssetCustodian.Status = Convert.ToString(dataReader["Status"]);
                }

                fixedAssetCustodians.Add(fixedAssetCustodian);
            }
            dataReader.Close();
            return fixedAssetCustodians;
        }

        public SqlCommand ConstructCommand(SqlCommand command, FixedAssetCustodian fixedAssetCustodian)
        {
            command.CommandType = CommandType.StoredProcedure;


            try
            {
                command.Parameters.AddWithValue("@TransactionId", fixedAssetCustodian.TransactionId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@CompanyId", fixedAssetCustodian.CompanyId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelListId", fixedAssetCustodian.OrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", fixedAssetCustodian.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ItemSerialCode", fixedAssetCustodian.ItemSerialCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@PastOwner", fixedAssetCustodian.PastOwner);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@NewOwner", fixedAssetCustodian.NewOwner);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@EffectiveDate", fixedAssetCustodian.EffectiveDate);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Status", fixedAssetCustodian.Status);
            }
            catch (Exception)
            { }
            return command;
        }
    }

}
